/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MessageBox2.java
 *
 * Created on 25.08.2011, 2:04:22
 */
package com.tsystems.school.simpleclient;

import com.tsystems.school.utils.*;
import java.awt.Color;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author 123
 */
public class MessageBox2 extends javax.swing.JFrame {

    /** Creates new form MessageBox2 */
    private ClientWorker cw;
    private MessagePane selectedPane;
    private HashMap<String, MessagePane> tabMap;
    public MessageBox2() {
        tabMap = new HashMap<String, MessagePane>();
        
        initComponents();
    }
    public MessageBox2(ClientWorker cw) {
        this();
        this.cw = cw;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        sendButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTabbedPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        sendButton.setText("Отправить");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(321, Short.MAX_VALUE)
                .addComponent(sendButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(sendButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        selectedPane = (MessagePane)jTabbedPane1.getSelectedComponent();
        String msg = selectedPane.getTextForSending();
        Date date = new Date();
        selectedPane.addMessagePane("me", msg + "\n", Color.BLUE, date);
        selectedPane.writeToHistory("me", msg, date);
        cw.sendMessage(new Message("me", selectedPane.getContact().getLogin(), MessageType.MESSAGE, msg));
        selectedPane.clearTyping();
    }//GEN-LAST:event_sendButtonActionPerformed


    
    public void addTab(Contact contact) {
        MessagePane mpane =  new MessagePane(contact);
        jTabbedPane1.add(contact.getLogin(), mpane);
        jTabbedPane1.setSelectedComponent(mpane);
        tabMap.put(contact.getLogin(), mpane);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MessageBox2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton sendButton;
    // End of variables declaration//GEN-END:variables



    HashMap<String, MessagePane> getTabMap() {
        return tabMap;
    }

    JTabbedPane getTabPane() {
        return jTabbedPane1;
    }

    
}
